package com.wang.mall.controller;

import com.wang.mall.model.R;
import com.wang.mall.service.CartService;
import com.wang.mall.vo.CartRedisVo;
import com.wang.mall.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/cart")
public class CartController {

    @Autowired
    private CartService cartService;


    /**
     * 删除购物车商品
     * @param productId
     * @return
     */
    @GetMapping("/delProduct")
    public R delProduct(Long productId) {
        boolean isOk = cartService.delProduct(productId);
        return R.ok(isOk);
    }

    @GetMapping("/updateProductCount")
    public R updateProductCount(Long productId, Integer productCount) {
        boolean isOk = cartService.updateProductCount(productId, productCount);
        return R.ok(isOk);
    }


    // 购物车商品选中与不选中
    @GetMapping("/checkItem")
    public R checkItem(@RequestParam("productId") Long productId, @RequestParam("check") Boolean check) {
        boolean isOK = cartService.checkItem(productId, check);
        return R.ok(isOK);
    }

    /**
     * 获取当前用户购物车中的数据
     *
     * @return
     */
    @GetMapping("/getCurrentMemberCartData")
    public R<CartRedisVo> getCurrentMemberCartData() {
        CartRedisVo cartRedisVo = cartService.getCurrentMemberCartData();
        return R.ok(cartRedisVo);
    }

    @PostMapping("/addCart")
    public R addCart(@RequestBody CartVo cartVo) {
        boolean cartItem = cartService.addCart(cartVo);
        return R.ok(cartItem);
    }
}
